package com.design.YiTong.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.design.YiTong.model.article.articleInfo;
import com.design.YiTong.model.medicinal.MedicinalOrder;
import com.design.YiTong.model.medicinal.MedicinalShop;
import com.design.YiTong.shop.mapper.MedicinalMapper;
import com.design.YiTong.shop.service.ShopService;
import org.springframework.stereotype.Service;

import java.io.*;

@Service
public class ShopServiceImpl extends
        ServiceImpl<MedicinalMapper,MedicinalShop> implements ShopService {

    @Override
    public MedicinalShop searchById(long id) {
        MedicinalShop medicinalShop=baseMapper.selectById(id);
        String url="";
        String image="";
        if(medicinalShop.getImage()!=null&&!medicinalShop.getImage().equals("")) {
            try {
                BufferedReader br = new BufferedReader(new FileReader(medicinalShop.getImage()));
                while (true) {
                    url = br.readLine();//readLine()读入一行后会自动换行
                    if (url==null) break;
                    image+=url;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        medicinalShop.setImage(image);
        return medicinalShop;
    }

    @Override
    public boolean updateMedinalSum(MedicinalOrder medicinalOrder) {
        QueryWrapper<MedicinalShop>wrapper=new QueryWrapper<>();
        wrapper.eq("id",medicinalOrder.getMedicinal_id());
        MedicinalShop medicinalShop=baseMapper.selectOne(wrapper);
        if(medicinalOrder.getNums()>medicinalShop.getAvailableNumber())return false;
        medicinalShop.setAvailableNumber(medicinalShop.getAvailableNumber()-medicinalOrder.getNums());
        baseMapper.update(medicinalShop,wrapper);
        return true;
    }

    @Override
    public IPage<MedicinalShop> showMedicinal(Page<MedicinalShop> pageParam) {
        QueryWrapper<MedicinalShop>wrapper=new QueryWrapper<>();
        IPage<MedicinalShop> pages = baseMapper.selectPage(pageParam,wrapper);
        return pages;
    }

    @Override
    public IPage<MedicinalShop> searchByName(Page<MedicinalShop> pageParam,String name) {
        QueryWrapper<MedicinalShop>wrapper=new QueryWrapper<>();
        wrapper.like("medname",name);
        IPage<MedicinalShop>pages=baseMapper.selectPage(pageParam,wrapper);
        return pages;
    }
    @Override
    public void saveOrupdataMed(MedicinalShop medicinalShop) {
        MedicinalShop Info=searchById(medicinalShop.getId());
        File file=new File(Info.getId().toString());
        if(Info==null){
            if(medicinalShop.getImage()!=null) {
                try {
                    file.createNewFile();
                    FileWriter write = new FileWriter("Med"+Info.getId().toString());
                    BufferedWriter bw = new BufferedWriter(write);
                    bw.write(medicinalShop.getImage());
                    bw.close();
                    write.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            medicinalShop.setImage("Med"+Info.getId().toString());
            baseMapper.insert(medicinalShop);
        }
        if(medicinalShop.getImage()!=null) {
            try {
                FileWriter write = new FileWriter("Med"+Info.getId().toString());
                BufferedWriter bw = new BufferedWriter(write);
                bw.write(medicinalShop.getImage());
                bw.close();
                write.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        medicinalShop.setImage("Med"+Info.getId().toString());
        baseMapper.updateById(medicinalShop);
    }
}
